import pymysql


class User(object):
    def __init__(self):
        self._user_name = ''
        self._phone_number = '11111111111'
        self._grade = 0
        self._feature_export_to_word = 0

    @property
    def user_name(self):
        return self._user_name

    @user_name.setter
    def user_name(self, value):
        if not isinstance(value, str):
            raise TypeError('Expected type str.')
        self._user_name = value

    @property
    def grade(self):
        return self._grade

    @grade.setter
    def grade(self, value):
        if not isinstance(value, int):
            raise TypeError('Expected type int.')
        self._grade = value

    @property
    def phone_number(self):
        return self._phone_number

    @phone_number.setter
    def phone_number(self, value):
        if not isinstance(value, str):
            raise TypeError('Expected type str.')
        self._phone_number = value

    @property
    def feature_export_to_word(self):
        return self._feature_export_to_word

    @feature_export_to_word.setter
    def feature_export_to_word(self, value):
        if not isinstance(value, int):
            raise TypeError('Expected type int.')
        self._feature_export_to_word = value


class Server(object):
    def __init__(self):
        self.db = None
        self.cursor = None
        self.server_name = 'cdb-7uvu1qdl.gz.tencentcdb.com' #cd-cdb-rgox5k2q.sql.tencentcdb.com
        self.server_port = 10056  # 62628
        self.server_user = 'root'
        self.server_pwd = 'Welcome1+'

    def connect_to_server(self):
        try:
            # "FunnyExam", charset='utf8
            self.db = pymysql.connect(host=self.server_name, port=self.server_port,
                                      user=self.server_user, password=self.server_pwd,
                                      database='FunnyExam', connect_timeout=0.5)
        except Exception as e:
            print(e)
            return False
        if self.db is None:
            return False
        self.cursor = self.db.cursor()
        return True

    def close(self):
        try:
            if self.db is not None:
                self.cursor.close()
                self.db.commit()
                self.db.close()
        except Exception as e:
            pass

    def get_user_info(self, phone):
        user = User()
        sql = 'select user_name, phone_number, grade, feature_export_to_word from user where phone_number={0}'.format(phone)
        effect_row = self.cursor.execute(sql)
        if effect_row == 0:
            return None
        data = self.cursor.fetchone()
        user.user_name = data[0]
        user.phone_number = data[1]
        user.grade = data[2]
        user.feature_export_to_word = data[3]
        return user

    def add_new_user(self, user):
        sql = 'insert into user (user_name, phone_number, grade) values (\'{0}\', \'{1}\', {2})'.\
            format(user.user_name, user.phone_number, user.grade)
        try:
            effect_row = self.cursor.execute(sql)
        except Exception as e:
            print(e)
            return False

        if effect_row != 1:
            return False
        self.db.commit()
        return True

    def get_advertise_info(self, ind):
        data = []
        sql = 'select content, publisher, contact from advertising_board where enable={0} and ind={1}'.format(1, ind)
        try:
            effect_row = self.cursor.execute(sql)
            if effect_row == 0:
                return None
            data = self.cursor.fetchone()
        except Exception as e:
            print(e)
            return None
        content = data[0]
        publisher = data[1]
        contact = data[2]
        return content, publisher, contact
